package com.momo.demo09_hightlighter;

import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.util.Version;
import org.junit.Test;

import com.momo.javabean.Article;
import com.momo.utils.DocumentUtils;
import com.momo.utils.LuceneUtils;

/*
 * 1��ʹ�ؼ��ֱ�ɫ <font color='red'>ղķ˹</font>
 * 2������ժҪ�Ĵ�С
 */
public class HightlighterTest {
    @Test
    public void testSearchIndex() throws Exception {
        IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory);
        QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30, new String[]{"title", "content"}, LuceneUtils.analyzer);
        Query query = queryParser.parse("�ܹھ�");
        TopDocs topDocs = indexSearcher.search(query, 25);
        int count = topDocs.totalHits;// �ܵļ�¼��
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        List<Article> articleList = new ArrayList<Article>();

        // ���ø�����
        Formatter formatter = new SimpleHTMLFormatter("<font color='red'>", "</font>");// ���ؼ���ָ��ǰ׺�ͺ�׺
        Scorer scorer = new QueryScorer(query);// ָ���ؼ���
        Highlighter highlighter = new Highlighter(formatter, scorer);
        Fragmenter fragmenter = new SimpleFragmenter(50);// ��ѯ������ʾ���ݵĳ���
        highlighter.setTextFragmenter(fragmenter);

        for (int i = 0; i < scoreDocs.length; i++) {
            int index = scoreDocs[i].doc;
            Document document = indexSearcher.doc(index);
            // ��һ������ �ҵ��ؼ��� �ڶ������� ���ĸ��ֶεĹؼ��ʽ��и���
            String titleText = highlighter.getBestFragment(LuceneUtils.analyzer, "title", document.get("title"));
            String contentText = highlighter.getBestFragment(LuceneUtils.analyzer, "content", document.get("content"));
            Article article = DocumentUtils.document2Article(document);
            if (titleText != null || !titleText.equals("")) {// ��֤Ҫ�������ֶ����йؼ���
                article.setTitle(titleText);
            }
            if (contentText != null || !contentText.equals("")) {
                article.setContent(contentText);
            }
            articleList.add(article);
        }

        // �����ѯ���
        for (Article article : articleList) {
            System.out.println(article.getId());
            System.out.println(article.getTitle());
            System.out.println(article.getContent());
        }
    }
}
